package com.paomi.client.activity.login;

import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.login.presenter.LoginPresenter;
import com.paomi.client.activity.login.view.LoginMvpView;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.bean.UserInfo;
import com.paomi.client.bean.UserUtils;
import com.paomi.client.utils.RxRegUtils;
import com.paomi.client.widget.RxToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by pc on 2018/10/24
 * Describe:
 * com.paomi.client.activity.login
 */
public class LoginActivity extends BaseMvpActivity<LoginMvpView, LoginPresenter> implements LoginMvpView {

    @BindView(R.id.editview_phone)
    EditText editviewPhone;
    @BindView(R.id.et_cord)
    EditText et_cord;
    @BindView(R.id.tv_code)
    TextView tv_code;
    @BindView(R.id.tv_login)
    TextView tv_login;
    @BindView(R.id.button_goback)
    TextView button_goback;

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initEvent() {
        button_goback.setVisibility(View.VISIBLE);
        button_goback.setTypeface(iconfont);
    }

    @OnClick(R.id.button_goback)
    void back() {
        finish();
    }

    @Override
    protected String setTitle() {
        return "登录";
    }

    @Override
    protected void showBack(TextView button_goback) {
    }

    @OnClick(R.id.tv_code)
    void getCord() {
        if (("").equals(editviewPhone.getText().toString().trim())) {
            RxToast.info(getResources().getString(R.string.input_phoneNum));
            return;
        }
        String phone = editviewPhone.getText().toString().trim();
        if (phone.length() < 6 || phone.length() > 12 || !RxRegUtils.isMobileExact(phone)) {
            RxToast.info(getResources().getString(R.string.input_phoneNu_true));
            return;
        }
        getPresenter().setSendMsg(editviewPhone.getText().toString().trim());
        countDownTimer.start();
    }

    /**
     * 2018/7/4/004 10:50
     * 验证码倒计时
     */
    private CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            long time = millisUntilFinished / 1000;
            tv_code.setText(time + "秒后重新获取");
            tv_code.setClickable(false);
        }

        @Override
        public void onFinish() {
            tv_code.setText(getResources().getString(R.string.input_cord_again));
            tv_code.setClickable(true);
        }
    };

    @Override
    protected void onDestroy() {
        countDownTimer.cancel();
        countDownTimer.onFinish();
        countDownTimer = null;
        super.onDestroy();
    }

    @NonNull
    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    /**
     * 登录成功后返回的信息
     *
     * @param result
     */
    @Subscribe
    public void onEvent(UserInfo result) {
    }

    @OnClick(R.id.tv_login)
    public void onViewClicked() {
        getPresenter().setLogin(editviewPhone.getText().toString().trim(), et_cord.getText().toString().trim());
    }

    /**
     * 发送验证码成功
     *
     * @param result
     */
    @Override
    public void showSendMsg(BaseJSON result) {
        Log.e("returnMessage", "" + result.returnMessage);
    }

    /**
     * 登录成功的操作
     *
     * @param result
     */
    @Override
    public void showLoginData(UserInfo result) {
        Log.e("UserInfo", "" + result.getReturnData());
        result.setMobile(editviewPhone.getText().toString().trim());
        UserUtils.getInstance().saveUser(result);
        getPresenter().getUserInfo();
    }

    @Override
    public void showUserInfo(UserInfo result) {
        UserUtils.getInstance().saveUser1(result);
        finish();
    }
}
